package 算法学习计划;

public class 插入的位置 {

    public static int searchInsert(int[] nums, int target){
        short mid = (short) ((nums.length - 1) / 2);
        short start = 0;
        short end = (short) (nums.length - 1);
        while (end >= start){

            if (nums[mid] == target){
                return mid;
            }
            if (nums[mid] > target){
                end = (short) (mid - 1);
                mid = (short) ((end - start)/ 2);
            }else {
                start = (short) (mid + 1);
                mid = (short) (start + ((end - start)/ 2));
            }
        }
        if (start >= nums.length){
            start = (short) (nums.length - 1);
        }

        return nums[start] > target ? start : start + 1;
    }

    public static void main(String[] args) {
        int[] nums = {3,5,7,9,10};
        System.out.println(searchInsert(nums,8));
    }
}
